package com.th.supcom.pds.drugreturn.server;

import java.util.ArrayList;
import java.util.List;

import net.carefx.framework.container.client.exception.UiException;
import net.carefx.framework.util.ConvertUtils;
import net.carefx.framework.web.gwt.server.GwtController;

import com.th.supcom.pds.domain.vo.PatientInfo;
import com.th.supcom.pds.domain.vo.PdsPrescDetailAndPrescVo;
import com.th.supcom.pds.domain.vo.PrescAndPatiResult;
import com.th.supcom.pds.drugreturn.client.service.RefundApplicationRpc;
import com.th.supcom.pds.internal.api.RefundApplicationService;
import com.th.supcom.pds.internal.client.model.MedicareCardPatientInfoModel;
import com.th.supcom.pds.internal.client.model.PatientInfoModel;
import com.th.supcom.pds.internal.client.model.PrescAndPatResultModel;
import com.th.supcom.pds.sortingdrug.client.model.PdsPrescDetailInfoModel;

/**
 * 门诊退药控制层.
 * 
 * @author yzhang
 */
public class RefundApplicationServiceController extends GwtController implements RefundApplicationRpc
{

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, TH Supcom Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

    /**
     * 序列化
     */
    private static final long serialVersionUID = 3615105067325287936L;

    private RefundApplicationService m_refundApplicationService;

    /**
     * 默认构造函数.
     */
    public RefundApplicationServiceController ()
    {

    }

    /**
     * 门诊退药通过
     */
    @Override
    public void refundApplicate (final List <PdsPrescDetailInfoModel> prescModels, final List <PdsPrescDetailInfoModel> prescDetailModels, final String operator)
                                                                                                                                                                 throws UiException
    {
        final List <PdsPrescDetailAndPrescVo> prescList = ConvertUtils.convert (PdsPrescDetailAndPrescVo.class, prescModels);
        final List <PdsPrescDetailAndPrescVo> prescDetailList = ConvertUtils.convert (PdsPrescDetailAndPrescVo.class, prescDetailModels);
        try
        {
            m_refundApplicationService.modifyrefundApplicate (prescList, prescDetailList, operator);
        }
        catch (final Exception e)
        {
            throw new UiException (e.getMessage (), e);
        }
    }

    public void setRefundApplicationService (final RefundApplicationService refundApplicationService)
    {
        m_refundApplicationService = refundApplicationService;
    }

    @Override
    public PrescAndPatResultModel findPrescDetailByPresc (List <String> prescNoList, String performedBy) throws UiException
    {
        List <List <PdsPrescDetailAndPrescVo>> lists;
        PrescAndPatResultModel prescAndPatResultModel = new PrescAndPatResultModel ();
        List <List <PdsPrescDetailInfoModel>> modelList = new ArrayList <List <PdsPrescDetailInfoModel>> ();
        // 存放主处方数据
        final List <PdsPrescDetailInfoModel> prescs = new ArrayList <PdsPrescDetailInfoModel> ();
        // 处方处方明细数据
        final List <PdsPrescDetailInfoModel> prescDetails = new ArrayList <PdsPrescDetailInfoModel> ();
        PrescAndPatiResult prescAndPatiResult = null;
        try
        {
            prescAndPatiResult = m_refundApplicationService.getPrescDetailForRefund (prescNoList, performedBy);
            lists = prescAndPatiResult.getList ();
        }
        catch (final Exception e)
        {
            throw new UiException (e.getMessage (), e);
        }
        if (lists == null || lists.isEmpty ())
        {
            return null;
        }

        for (List <PdsPrescDetailAndPrescVo> prescVoList : lists)
        {
            List <PdsPrescDetailInfoModel> prescModel = ConvertUtils.convert (PdsPrescDetailInfoModel.class, prescVoList);

            Double sum = 0.0;
            // 设置处方的总金额
            for (final PdsPrescDetailInfoModel model2 : prescModel)
            {
                sum += model2.getCosts ();
            }
            if (!prescModel.isEmpty ())
            {
                prescModel.get (0).setTotalPrice (sum);
            }
            prescs.add (prescModel.get (0));
            prescDetails.addAll (prescModel);
            modelList.add (prescs);
            modelList.add (prescDetails);
        }
        prescAndPatResultModel.setPdsPrescDetailInfoModellist (modelList);
        // 患者信息转换
        if (null != prescAndPatiResult.getPatientInfo ())
        {
            PatientInfo patientIno = prescAndPatiResult.getPatientInfo ();
            PatientInfoModel medicareCardAllInfoModel = new PatientInfoModel ();
            MedicareCardPatientInfoModel medicareCardPatientInfoModel = new MedicareCardPatientInfoModel ();
            medicareCardPatientInfoModel.setName (patientIno.getName ());
            medicareCardPatientInfoModel.setSex (patientIno.getSex ());
            medicareCardPatientInfoModel.setDateOfBirth (patientIno.getBirthDate ());
            medicareCardPatientInfoModel.setPatientId (patientIno.getPatiId ());
            medicareCardPatientInfoModel.setUpdateTime (patientIno.getVisitBeginDate ());
            medicareCardPatientInfoModel.setChargeType (patientIno.getChargeType ());
            medicareCardPatientInfoModel.setVisitId (patientIno.getVisitId ());
            medicareCardPatientInfoModel.setDeptName (patientIno.getDeptName ());
            medicareCardPatientInfoModel.setVisitDay (patientIno.getVisitDay ());
            medicareCardPatientInfoModel.setBedCode (patientIno.getBedCode ());

        }
        modelList.add (prescs);
        modelList.add (prescDetails);
        return prescAndPatResultModel;
    }

}